package cn.tedu.charging.user.dao.repository.impl;


import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.user.dao.mapper.ChargingUserInfoMapper;

import cn.tedu.charging.user.dao.repository.UserRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private ChargingUserInfoMapper chargingUserInfoMapper;

    @Override
    public ChargingUserInfoPO getUserByOpenId(String openId) {
        //select * from charging_user_info where wx_open_id = #{openId}
        QueryWrapper<ChargingUserInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_open_id",openId);
        return chargingUserInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(ChargingUserInfoPO user) {
        chargingUserInfoMapper.insert(user);
    }

    @Override
    public ChargingUserInfoPO getUserById(Integer userId) {
            return chargingUserInfoMapper.selectById(userId);
    }

    @Override
    public Double getUserBalance(Integer userId) {
        QueryWrapper<ChargingUserInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("id",userId);
        Double balance = chargingUserInfoMapper.selectOne(wrapper).getBalance();

        return balance;
    }

    @Override
    public void charge(ChargeParam param) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",param.getUserId());
        updateWrapper.setSql("balance = balance + "+param.getAmount());
        chargingUserInfoMapper.update(null,updateWrapper);
    }


}
